package com.mall_main.controller;

import com.common.result.JsonResult;
import com.common.result.ResultEnum;
import com.mall_main.pojo.cache.AddShoppingCarCache;
import com.mall_main.pojo.dto.DeleteGoodsDTO;
import com.mall_main.pojo.dto.ShoppingCarDTO;
import com.mall_main.service.MallService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Validated
@RestController
@RequestMapping("v1/mall")
public class MallController {

    @Autowired
    MallService mallService;
    @PostMapping("get")
    public JsonResult getMall(){
        return JsonResult.ok();
        //todo
    }

    @PostMapping("/getUserShoppingCar")
    public JsonResult getUserShoppingCar(long uid){
       List<AddShoppingCarCache> addShoppingCarCaches = mallService.getShoppingCar(uid);
        return new JsonResult(ResultEnum.SECCESS, addShoppingCarCaches);
    }
    @PostMapping("/addUserShoppingCar")
    public JsonResult addUserShoppingCar(ShoppingCarDTO shoppingCarDTO){
        return mallService.addUserShoppingCar(shoppingCarDTO);
    }

    @PostMapping("/buy")
    @Transactional
    public JsonResult buy(List<ShoppingCarDTO> shoppingCarDTOS){


return JsonResult.ok();

    }
//
//    @PostMapping("test1")
//    public int test1(){
//        return mallService.test1();
//    }
//    @PostMapping("test2")
//    public int test2(){
//        return  mallService.test2();
//    }

    @Transactional
    @PostMapping("createOrder")
    public JsonResult createOrder(List<ShoppingCarDTO> shoppingCarDTOS)  {
        try {
            return mallService.createOrder(shoppingCarDTOS);
        } catch (NoSuchFieldException e) {
            return new JsonResult(ResultEnum.ERROR_UNKNOWN);
        }
    }



    @PostMapping("/deleteGoods")
    public JsonResult deleteGoods(long uid,List<DeleteGoodsDTO> deleteGoodsDTOS){
        return mallService.deleteGoods(uid,deleteGoodsDTOS);

    }




}
